package net.aaa.hotel.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import net.aaa.hotel.dao.UserDao;
import net.aaa.hotel.dao.impl.UserDaoImpl;
import net.aaa.hotel.entity.Custom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 凉凉
 * @time
 */

@RestController
@RequestMapping("/User")
public class UserFController {

    @Autowired
    private UserDao userDao=new UserDaoImpl();


    @RequestMapping("index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("qianduan/index.jsp");
        return modelAndView;
    }

    @RequestMapping("/queryAll")
    public Map queryAll(HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws ServletException, IOException {
        Map cuser = (Map) session.getAttribute("cuser");
   //     System.out.println("cuser"+cuser);
        //cuser{img=9add177a170a.jpg, idcard=410270199910120519, name=周晨222, id=1, pwd=111111, iphone=13033979752, status=0}
        //cuser{idcard=123, name=123, id=8, pwd=1312, iphone=123, status=0}
      //  System.out.println("cuser"+cuser.get("id"));
        List<Map> query= userDao.query(cuser);
        return query.get(0);
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(Custom xinxi,HttpSession session){
     //   System.out.println("xinxi1111"+xinxi);
    //    System.out.println(xinxi.getId());
        int update1 = userDao.update(xinxi);
        session.setAttribute("user",xinxi);
        Map user = (Map) session.getAttribute("user");
        session.setAttribute("cuser",user);
      return "1";
    }



    @RequestMapping("/upl")
    public Map getUploadPath(MultipartFile file) {
        String path = "";
        Map ma = new HashMap();

        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        UUID uuid = UUID.randomUUID();
        String objectName = uuid + suffix;
        try {
            // Endpoint以杭州为例，其它Region请按实际情况填写。
            //
            //oss-cn-beijing.aliyuncs.com
            String endpoint = "oss-cn-beijing.aliyuncs.com";
            // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
            String accessKeyId = "LTAI4GCzjhtjNLVfjx8BPm1h";
            String accessKeySecret = "dmXsWFjrdNanypvoLXYuDsuO56slhP";
            String bucketName = "yuyongli";


            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream);

            // 关闭OSSClient。
            ossClient.shutdown();

            path = "https://" + bucketName + "." + endpoint + "/" + objectName;
            //文件上传需要的参数  msg code data
            ma.put("code", 0);
            ma.put("msg", "");
            ma.put("data", path);


        } catch (IOException e) {
            e.printStackTrace();

        }

        return ma;
    }


}
